// pages/all_words/index.js
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		words: [],
		hiddenText: {
			0: 'A',
			1: 'W',
			2: 'M'
		},
		hidden: 0,
		mode: 0,
	},

	editWord(e) {
		const gonnaEditWord = e.currentTarget.dataset
		console.log(gonnaEditWord)
		const meaning = []
		for (const key in gonnaEditWord.meaning) {
			const means = gonnaEditWord.meaning[key]
			const current = meaning.find(i => i.tag === key)
			if (!current) {
				meaning.push({ tag: key, translation: means.join(',') })
			} else {
				current.translation += means.join(',')
			}
		}
		const data = {
			meaning,
			word: gonnaEditWord.spell,
			wordId: gonnaEditWord.wordid,
		}
		// 记得这里要传type=1，这样能显示单词页面
		wx.navigateTo({
			url: `../input/index?type=1&word=${
				data.word
			}&meaning=${JSON.stringify(data.meaning)}&wordid=${data.wordId}`,
		})
	},

	changeHidden() {
		let { hidden } = this.data
		hidden = hidden < 2 ? ++hidden : 0
		this.setData({ hidden }) 
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		const visionMode = wx.getStorageSync('vision-mode')
		this.setData({ mode: visionMode })
		wx.showLoading({
			title: 'Searching...',
			mask: true,
		})
		wx.cloud
			.callFunction({ name: 'getMyWord' })
			.then(
				words => {
					console.log({ words })
					this.setData({ words: words.result.data })
				},
				() => {
					wx.showToast({
						title: 'Some Error Happends',
						icon: 'error',
						mask: true,
					})
					wx.navigateBack({ delta: 1 })
				}
			)
			.finally(() => {
				wx.hideLoading()
			})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {},
})
